<?php


namespace app\admin\model;


use app\common\lib\RespCode;
use app\common\model\BaseModel;
use custom\Err;
use think\Db;

class MessageSystem extends BaseModel {

    /**
     * 列表
     * @param $params
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getList($params) {
        $map = [];
        if (!empty($params['title'])) {
            $map[] = ['ms.title', 'like', '%' . trim($params['title']) . '%'];
        }
        if (!empty($params['start_time']) && !empty($params['end_time'])) {
            $map[] = ['ms.itime', 'between', [strtotime($params['start_time']), strtotime($params['end_time'])+86399]];
        }
        $count = self::alias('ms')
            ->leftJoin('admin a', 'a.id = ms.writer_id')
            ->where($map)
            ->count();
        if (empty($count)) {
            $result = [
                'page'    => 1,
                'pageNum' => 1,
                'count'   => 0,
                'data'    => [],
            ];
        } else {
            $page = empty($params['page']) ? 1 : intval($params['page']);
            $long = empty($params['long']) ? 10 : intval($params['long']);
            $data = self::alias('ms')
                ->leftJoin('admin a', 'a.id = ms.writer_id')
                ->page($page, $long)
                ->order('ms.id', 'desc')
                ->field('ms.*,a.nickname')
                ->where($map)
                ->select()
                ->toArray();
            foreach ($data as $k => $v) {
                $data[$k]['type'] = $data[$k]['type'] == 1 ? 'app内开' : 'h5网页';
                $data[$k]['itime'] = date('Y-m-d H:i:s',$data[$k]['itime']);
                $data[$k]['is_push'] = $data[$k]['is_push'] == 1 ? '推送成功' : '待推送';
            }
            $pageNum = ceil($count / $long);
            $result = [
                'page'    => $page,
                'pageNum' => $pageNum,
                'count'   => $count,
                'data'    => $data,
            ];
        }
        return $result;
    }

    /**
     * 编辑、添加
     * @param $params
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function post($params) {
        if(empty(trim ($params['title']))){
            Err::add('标题不能为空', RespCode::PARAMS_ERR);
            return false;
        }
        if(empty(trim ($params['content']))){
            Err::add('内容不能为空', RespCode::PARAMS_ERR);
            return false;
        }
        if (mb_strlen(trim($params['title']), 'utf-8') > 30) {
            Err::add('标题长度限制30', RespCode::PARAMS_ERR);
            return false;
        }
        if (mb_strlen(trim($params['content']), 'utf-8') > 100) {
            Err::add('内容长度限制100', RespCode::PARAMS_ERR);
            return false;
        }
        if(!empty($params['link'])){
            if (mb_strlen(trim($params['link']), 'utf-8') > 200) {
                Err::add('链接长度限制200', RespCode::PARAMS_ERR);
                return false;
            }
            if(empty($params['pic'])){
                $params['pic']="https://oss.fpaiyun.com/admin/h5_logo/2021-01-19/35pr1f548cce.png";
            }
        }
        if(!empty($params['link_title'])){
            if (mb_strlen(trim($params['link_title']), 'utf-8') > 30) {
                Err::add('链接编辑长度限制30位', RespCode::PARAMS_ERR);
                return false;
            }
        }
        $data = [
            'title'     => trim($params['title']),
            'content'   => trim($params['content']),
            'link'      => trim($params['link'])??'',
            'link_title'      => trim($params['link_title'])??'',
            'pic'      => trim($params['pic'])??'',
            'type'      => 2,
            'is_push'   => 0,
            'msg_type'  => 1,
            'writer_id' => $params['user_id'],
        ];
        if (empty($params['id'])) {
            $data['itime'] = time();
            self::insert($data);
        } else {
            self::where('id', $params['id'])->update($data);
        }
        return true;
    }

    /**
     * 删除
     * @param $params
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public static function del($params) {
        if (!self::where('id', $params['id'])->select()->toArray()) {
            Err::add('数据不存在', RespCode::PARAMS_ERR);
            return false;
        }
        self::where('id', $params['id'])->delete();
        return true;
    }

    /**
     * 详情
     * @param $id
     * @param $_info
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function info($id,&$_info){
        $info = self::where('id', $id)
            ->select()
            ->toArray ();
        if(empty($info)){
            Err::add('数据不存在', RespCode::PARAMS_ERR);
            return false;
        }
        $_info=$info[0];
        return true;
    }
}
